const {
  http
} = require("../../../../utils/apiutils");
const util = require("../../../../utils/util")
const app = getApp()

Page({
  /**
   * 页面的初始数据
   */
  data: {
    CustomBar: 0,
    statusBar: 0,
    showcollapse: false,
    collapse: false,
    fpx:0,
    steps: [{
        text: '2020.11.22',
        desc: '开始报名',
      },
      {
        text: '2020.11.22',
        desc: '截止报名',
      },
      {
        text: '2020.11.22',
        desc: '工作时间',
      },
      {
        text: '2020.11.22',
        desc: '工作结束',
      },
    ],
    jobData: {},
    latitude: 23.095994,
    longitude: 113.325520,
    markers: [],
    isdescription: false,
    userInfo:{},
    isCanDraw: false,
    obj: {
      title:'',
      money:'',
      width:'750px',
      height:'1194px',
      code_width:'150px',
      code_height:'150px',
      left:'500px',
      top:'814px',
      first_img:'',
      btn_background:'#5B0EFE'
    },
		bgImg: 'https://linghuoimage.xbbzp.com/FhNVzlBo6mdk0Z-wjPI0pCi3lqjE',
		code_img: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(getCurrentPages())

    let CustomBar = app.globalData.CustomBar;
    let statusBar = app.globalData.statusBar
    this.setData({
      CustomBar: CustomBar - statusBar,
      statusBar: statusBar,
    })
    var that = this;
    wx.getSystemInfo({
      success: (result) => {
        var spx = 280 / 750 * result.windowWidth;
        var fpx = 500 / 750 *result.windowWidth;
        that.setData({
          sh: result.windowHeight - CustomBar,
          shs: result.windowHeight - CustomBar - spx,
          fpx:fpx
        })
        console.log("-----宽度-----",fpx)
      },
    })
    let id = options.id;
    console.log("------工作的id----",id, )
    this.setData({
      id: id,
    })
   

  },

  callouttap: function () {
    wx.openLocation({
      latitude: Number(this.data.jobData.lat),
      longitude: Number(this.data.jobData.lng),
      name: this.data.jobData.workPlace,
      address: this.data.jobData.workPlace,
    })
  },
  //获取工作详情信息
  getJobData: function (id) {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    http.getJobData(id,
      function (res) {
        wx.hideLoading()
        console.log("------岗位详情---", res);
        if (res.success) {
          let db = res.data;
          var data = {
            id: 4,
            latitude: db.lat,
            longitude: db.lng,
            width: 30,
            height: 30,
            iconPath: '../../resources/location.png',
            customCallout: {
              anchorY: 0,
              anchorX: 20,
              display: 'ALWAYS',
            },
          }
          db.images = db.image.split(",")
          that.setData({
            jobData: db,
            isdescription: true,
            markers: [data],
            ['obj.first_img']:db.images[0],
            ['obj.title']:db.name,
            ['obj.money']:db.basicSalary/100+'元/'+db.payType,
            ['obj.company']:db.factoryName,
          })
          that.setCollapse()
        } else {
          wx.showToast({
            title: res.message,
            icon: "none",
          })
        }
      },
      function (err) {
        wx.hideLoading()
      })
  },

  markertap(e) {
    console.log('@@@ markertap', e)
  },

  oncliclCall: function (e) {

      wx.makePhoneCall({
        phoneNumber: this.data.jobData.contactPhone //仅为示例，并非真实的电话号码
      })

  },
  onclickBack: function (e) {

    let arr = getCurrentPages()
    if(arr>1)
    {
      wx.navigateBack({
        delta: 1,
      })
    }
    else
    {
      wx.switchTab({
        url: '/pages/tabBar/home/index',
      })
    }
  },

  onclickjob_favorite: function (e) {

    if (this.data.jobData.favorite) {
      var that = this;
      wx.showLoading({
        title: '加载中...',
      })
      http.job_favoriteD(this.data.jobData.id,
        function (res) {
          wx.hideLoading()
          if (res.code == 0) {
            wx.showToast({
              title: "取消成功",
              icon: "none",
            })

            that.setData({
              "jobData.favorite": false,
            })
          } else {
            wx.showToast({
              title: res.message,
              icon: "none",
            })
          }
        },
        function (err) {
          wx.hideLoading()
        })


    } else {
      var that = this;
      wx.showLoading({
        title: '加载中...',
      })
      http.job_favorite(this.data.jobData.id,
        function (res) {
          wx.hideLoading()
          if (res.code == 0) {
            wx.showToast({
              title: "收藏成功",
              icon: "none",
            })
            that.setData({
              "jobData.favorite": true,
            })
          } else {
            wx.showToast({
              title: res.message,
              icon: "none",
            })
          }
        },
        function (err) {
          wx.hideLoading()
        })

    }



  },
  onclickNext: function () {

    getApp().globalData.workInfo = this.data.jobData;
    if(!app.globalData.userInfo.idNumber)
    {
      wx.showModal({
        title:"提示",
        content:"您还没有实名认证无法报名，是否去实名认证",
        confirmText:"去认证",
        success:function(res){
          if(res.confirm)
          {
            wx.navigateTo({
              url: '/pages/me/pages/realname/realname',
            })
          }
        }
      })
      return
    }
    // if (this.data.jobData.registration) {
    //   wx.showToast({
    //     title: '您已报名该岗位，无需重复报名',
    //     icon: "none",
    //   })
    //   return
    // }
    wx.navigateTo({
      url: '/pages/home/pages/apply/apply',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getJobData(this.data.id)
    this.setData({
      userInfo: getApp().globalData.userInfo,
    })
  },
  //判断全文收起是否展示
  setCollapse: function () {
    var query = wx.createSelectorQuery();
    var that = this;
    query.selectAll('#co').boundingClientRect(function (rect) {
      rect.forEach((v, i) => {
        console.log("------2222---", v);
        if (v.height > 60) { //判断高度,根据各种高度取折中
          that.setData({
            collapse: true,
            showcollapse: true
          })
        }
      })
    }).exec();
  },
  //点击全文收起
  changeCollapse: function () {
    this.setData({
      collapse: !this.data.collapse
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var that = this;
    // 设置菜单中的转发按钮触发转发事件时的转发内容
    let shareurl = this.data.jobData.shareUrl;
    var code = ""
    if (shareurl) {
      code = util.GetQueryString(shareurl).code
    }
  
    var shareObj = {
      title: "分享岗位", // 默认是小程序的名称(可以写slogan等)
      path: '/pages/index/index?type=work&id=' + this.data.jobData.id + "&code=" + code, // 默认是当前页面，必须是以‘/'开头的完整路径
      imageUrl: '', //自定义图片路径，可以是本地文件路径、代码包文件路径或者网络图片路径，支持PNG及JPG，不传入 imageUrl 则使用默认截图。显示图片长宽比是 5:4
      success: function (res) {
        // 转发成功之后的回调
        if (res.errMsg == 'shareAppMessage:ok') {

        }
      }
    }
    console.log("------------",shareObj);
    // 返回shareObj
    return shareObj;

  },
  createShareImage() {
		// this.setData({
		// 	isCanDraw: !this.data.isCanDraw
    // })
    this.getCode()
  },
  getCode(){
    let params ={
      path:'pages/index/index',
      param:'route=work&id='+this.data.jobData.jobId
    }
    http.customQRCode(params,res=>{
      this.setData({
        code_img:'data:image/png;base64,'+res
      })
      wx.nextTick(_=>{
        this.setData({
          isCanDraw: !this.data.isCanDraw
        })
      })
    },err=>{})
  },
})